package com.gxstnu.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.github.pagehelper.PageInfo;
import com.gxstnu.common.Result;
import com.gxstnu.entity.User;
import com.gxstnu.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;

    @GetMapping("/selectAll")
    public Result selectAll(User user){
        List<User> userList = userService.selectAll(user);
        return Result.success(userList);
    }

    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1")Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             User user) {
        PageInfo<User> userPageInfo = userService.selectPage(pageNum, pageSize, user);
        return Result.success(userPageInfo);
    }

    @PostMapping("/add")
    public Result add(@RequestBody User user){//@RequestBody接收前端json参数数据
        userService.add(user);
        return Result.success();
    }
    @PutMapping ("/update")
    public Result update(@RequestBody User user){//@RequestBody接收前端json参数数据
        userService.update(user);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){//@PathVariable是前端传回来的路径参数
        userService.deleteById(id);
        return Result.success();
    }
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<User> list){//@RquestBody 接收前端传来的 json数组
        userService.deleteBatch(list);
        return Result.success();
    }

    @GetMapping("/export")
    public void exportData(User user, HttpServletResponse response) throws Exception {
        //1.拿到所有数据
        List<User> userList = userService.selectAll(user);
        //2.构建所有对象
        ExcelWriter writer = ExcelUtil.getWriter(true);
        //3.设置中文表头
        writer.addHeaderAlias("username","账号");
        writer.addHeaderAlias("name","名称");
        writer.addHeaderAlias("phone","电话");
        writer.addHeaderAlias("email","邮箱");
        // 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
        writer.setOnlyAlias(true);

        //4.写出数据到Writer
        writer.write(userList);
        //5.设置输出文件名称
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("管理员信息", StandardCharsets.UTF_8);
        response.setHeader("Content-Disposition","attachment;filename" + fileName + ".xlsx");
        //写出到输出流，并关闭 writer
        ServletOutputStream out=response.getOutputStream();

        writer.flush(out);
        writer.close();
        out.close();
    }

    @PostMapping("/import")
    public Result importData(MultipartFile file) throws Exception{
        //1.拿到输入流，构建reader
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        //2.通过 reader读取excel数据
        reader.addHeaderAlias("账号","username");
        reader.addHeaderAlias("名称","name");
        reader.addHeaderAlias("电话","phone");
        reader.addHeaderAlias("邮箱","email");

        List<User> list = reader.readAll(User.class);
        //3.将数据写入数据库
        for(User user : list) {
            userService.add(user);
        }
        return Result.success();
    }

}
